package com.example.test.support;

import com.example.test.consts.RedisConst;
import io.netty.util.internal.StringUtil;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class UserSupport {

    private StringRedisTemplate redisTemplate;

    // 从redis缓存中获取依赖
    public Integer getUserId(HttpServletRequest request){
        String token = request.getHeader("token");
        String tokenKey = RedisConst.LOGIN_TOKEN + token;
        String userId = redisTemplate.opsForValue().get(tokenKey);

        if (StringUtil.isNullOrEmpty(userId)) {
            // todo try catch 下
            throw new RuntimeException();
        } else {
            return Integer.valueOf(userId);
        }
    }
}
